import 'package:flutter/material.dart';
import 'package:wuliu01/utils/http.dart';
import 'package:wuliu01/utils/toast.dart';
import 'package:wuliu01/utils/token.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  //获得表单数据
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _codeController = TextEditingController();

  //按钮颜色
  bool isPressed = false;

  //使用token
  final tokenManager = TokenManager();

  //发送请求获取数据
  void getNotifyList() async {
    try {
      // 请求数据
      var res = await http.post('/driver/login/account', {
        'account': _phoneController.text,
        'password': _codeController.text,
      });
      //debugPrint('请求成功：$res');
      await TokenManager().init();
      TokenManager().saveToken(res['data']);
      ToastUtil.showSuccess(tokenManager.getToken().toString());
    } catch (e) {
      debugPrint('请求失败：$e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      //表头
      appBar: AppBar(
        toolbarHeight: 150,
        title: const Text(
          '神领快递',
          style: TextStyle(fontWeight: FontWeight.w600),
        ),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      //登录内容
      body: Container(
        // alignment: Alignment.center,
        padding: const EdgeInsets.only(
          left: 32,
          right: 17,
          top: 50,
          bottom: 46,
        ),
        child: Column(
          //mainAxisAlignment: MainAxisAlignment.start,
          children: [
            //账号登录+手机号登录
            Row(
              //中间
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  '账号登录',
                  style: TextStyle(fontWeight: FontWeight.w800, fontSize: 24),
                ),
                const Spacer(),
                Row(
                  children: [
                    Text(
                      '手机号登录',
                      style: TextStyle(fontSize: 16, color: Colors.red),
                    ),
                    Image.asset('ic_tab_btn_right.png'),
                  ],
                ),
              ],
            ),
            const SizedBox(height: 40),

            //输入
            Expanded(
              child: Column(
                children: [
                  TextField(
                    controller: _phoneController,
                    keyboardType: TextInputType.phone,
                    maxLength: 11,
                    style: const TextStyle(fontSize: 16),
                    decoration: const InputDecoration(
                      labelText: '请输入手机号',
                      hintText: '请输入手机号',
                    ),
                  ),
                  TextField(
                    controller: _codeController,
                    decoration: InputDecoration(
                      labelText: '请输入密码',
                      hintText: '请输入密码',
                    ),
                  ),
                  const SizedBox(height: 40),
                  //登录按钮
                  Row(
                    children: [
                      Expanded(
                        child: ElevatedButton(
                          style: ButtonStyle(
                            backgroundColor:
                                WidgetStateProperty.resolveWith<Color>((
                                  states,
                                ) {
                                  return states.contains(WidgetState.pressed)
                                      ? Color.fromARGB(255, 188, 146, 150)
                                      : Colors.red;
                                }),
                            minimumSize: WidgetStateProperty.all(Size(100, 50)),
                          ),
                          onPressed: () {
                            //按压赋值
                            setState(() {
                              isPressed = !isPressed;
                            });
                            //1.发送请求 2.存储token 3.跳转页面
                            Navigator.pushNamed(context, '/homePage');
                            getNotifyList();
                          },
                          child: const Text(
                            '登录',
                            style: TextStyle(color: Colors.white, fontSize: 20),
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
